package cn.ld.module.ai.service.model;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.ld.framework.common.enums.CommonStatusEnum;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;
import cn.ld.module.ai.controller.admin.model.vo.chatRole.AiChatRolePageInVO;
import cn.ld.module.ai.controller.admin.model.vo.chatRole.AiChatRoleSaveMyInVO;
import cn.ld.module.ai.controller.admin.model.vo.chatRole.AiChatRoleSaveInVO;
import cn.ld.module.ai.dal.entity.model.AiChatRoleEntity;
import cn.ld.module.ai.dal.mapper.model.AiChatRoleMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static cn.ld.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ld.framework.common.util.collection.CollectionUtils.convertList;
import static cn.ld.module.ai.enums.ErrorCodeConstants.*;

/**
 * AI 聊天角色 Service 实现类
 *
 * @author fansili
 */
@Service
@Slf4j
public class AiChatRoleServiceImpl implements AiChatRoleService {

    @Resource
    private AiChatRoleMapper chatRoleMapper;

    @Override
    public Long createChatRole(AiChatRoleSaveInVO createInVO) {
        AiChatRoleEntity chatRole = BeanUtils.toBean(createInVO, AiChatRoleEntity.class);
        chatRoleMapper.insert(chatRole);
        return chatRole.getId();
    }

    @Override
    public Long createChatRoleMy(AiChatRoleSaveMyInVO createInVO, Long userId) {
        AiChatRoleEntity chatRole = BeanUtils.toBean(createInVO, AiChatRoleEntity.class).setUserId(userId)
                .setStatus(CommonStatusEnum.ENABLE.getStatus()).setPublicStatus(false);
        chatRoleMapper.insert(chatRole);
        return chatRole.getId();
    }

    @Override
    public void updateChatRole(AiChatRoleSaveInVO updateInVO) {
        // 校验存在
        validateChatRoleExists(updateInVO.getId());
        // 更新
        AiChatRoleEntity updateObj = BeanUtils.toBean(updateInVO, AiChatRoleEntity.class);
        chatRoleMapper.updateById(updateObj);
    }

    @Override
    public void updateChatRoleMy(AiChatRoleSaveMyInVO updateInVO, Long userId) {
        // 校验存在
        AiChatRoleEntity chatRole = validateChatRoleExists(updateInVO.getId());
        if (ObjectUtil.notEqual(chatRole.getUserId(), userId)) {
            throw exception(CHAT_ROLE_NOT_EXISTS);
        }

        // 更新
        AiChatRoleEntity updateObj = BeanUtils.toBean(updateInVO, AiChatRoleEntity.class);
        chatRoleMapper.updateById(updateObj);
    }

    @Override
    public void deleteChatRole(Long id) {
        // 校验存在
        validateChatRoleExists(id);
        // 删除
        chatRoleMapper.deleteById(id);
    }

    @Override
    public void deleteChatRoleMy(Long id, Long userId) {
        // 校验存在
        AiChatRoleEntity chatRole = validateChatRoleExists(id);
        if (ObjectUtil.notEqual(chatRole.getUserId(), userId)) {
            throw exception(CHAT_ROLE_NOT_EXISTS);
        }
        // 删除
        chatRoleMapper.deleteById(id);
    }

    private AiChatRoleEntity validateChatRoleExists(Long id) {
        AiChatRoleEntity chatRole = chatRoleMapper.selectById(id);
        if (chatRole == null) {
            throw exception(CHAT_ROLE_NOT_EXISTS);
        }
        return chatRole;
    }

    @Override
    public AiChatRoleEntity getChatRole(Long id) {
        return chatRoleMapper.selectById(id);
    }

    @Override
    public List<AiChatRoleEntity> getChatRoleList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return chatRoleMapper.selectBatchIds(ids);
    }

    @Override
    public AiChatRoleEntity validateChatRole(Long id) {
        AiChatRoleEntity chatRole = validateChatRoleExists(id);
        if (CommonStatusEnum.isDisable(chatRole.getStatus())) {
            throw exception(CHAT_ROLE_DISABLE, chatRole.getName());
        }
        return chatRole;
    }

    @Override
    public PageResult<AiChatRoleEntity> getChatRolePage(AiChatRolePageInVO pageInVO) {
        return chatRoleMapper.selectPage(pageInVO);
    }

    @Override
    public PageResult<AiChatRoleEntity> getChatRoleMyPage(AiChatRolePageInVO pageInVO, Long userId) {
        return chatRoleMapper.selectPageByMy(pageInVO, userId);
    }

    @Override
    public List<String> getChatRoleCategoryList() {
        List<AiChatRoleEntity> list = chatRoleMapper.selectListGroupByCategory(CommonStatusEnum.ENABLE.getStatus());
        return convertList(list, AiChatRoleEntity::getCategory, role -> role != null && StrUtil.isNotBlank(role.getCategory()));
    }

    @Override
    public List<AiChatRoleEntity> getChatRoleListByName(String name) {
        return chatRoleMapper.selectListByName(name);
    }

}

